package com.congee02.bt.dfs.top2bottom;

import com.congee02.bt.TreeNode;

public class SumRootToLeaf {

    public int sumRootToLeaf(TreeNode root) {
        return process(root, 0);
    }

    private int process(TreeNode root, int prev) {

        if (root == null) {
            return 0;
        }

        int cur = prev * 2 + root.val;

        if (root.left == null && root.right == null) {
            return cur;
        }

        return process(root.left, cur) + process(root.right, cur);
    }

}
